#include<iostream>
#include<vector>
#include<queue>
using namespace std;



struct TreeNode {
      int val;
     TreeNode *left;
      TreeNode *right;
      TreeNode() : val(0), left(nullptr), right(nullptr) {}
      TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
      TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  };


class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        if (root == nullptr) return vector<int>();
        vector<int> ans;
        queue<TreeNode*> qu;
        qu.push(root);
        int flag = 0;
        while (!qu.empty()) {
            ans.push_back(qu.front()->val);
            int digit = qu.size();
            for (int i = 0; i < digit; i++) {
                TreeNode* tmpNode = qu.front();
                qu.pop();
                if (tmpNode->right != nullptr) qu.push(tmpNode->right);
                if (tmpNode->left != nullptr) qu.push(tmpNode->left);
            }
        }
        return ans;
    }
};